﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DbToDocument.Core.Util
{
    public static class EnumUtil
    {
        /// <summary>
        /// 方法扩展
        /// 用于获取枚举描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            if (value == null)
            {
                return null;
            }

            FieldInfo field = value.GetType().GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);
            if (field == null)
            {
                return null;
            }

            DescriptionAttribute customAttribute = field.GetCustomAttribute<DescriptionAttribute>(inherit: false);
            if (customAttribute != null && !string.IsNullOrEmpty(customAttribute.Description))
            {
                return customAttribute.Description;
            }

            return null;
        }

    }
}
